package ng.map;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;

import ng.graphics.Animation;
import ng.lib.Vector2D;

public abstract class MapObject {
	private static int MapObjectCounter = 0;

	protected final int id;

	protected Animation animation;
	protected double mass;
	protected Vector2D position;
	protected double angle; // multiple of pi

	public MapObject(int id, Animation a, Vector2D p) {
		this.id = id;
		this.animation = a;
		this.position = p;
	}

	public MapObject(Animation a, Vector2D p) {
		this.id = MapObjectCounter++;
		this.animation = a;
		this.position = p;
	}

	public MapObject() {
		this.id = MapObjectCounter++;
	}

	public MapObject(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setPosition(Vector2D a) {
		this.position = a;
	}

	public void setAngle(double a) {
		this.angle = a;
	}

	public Vector2D getPosition() {
		return this.position;
	}

	public double getAngle() {
		return this.angle;
	}

	public void tick() {
		this.animation.tick();
	}

	public double getMass() {
		return this.mass;
	}

	synchronized public void paintComponent(Graphics2D g2) {
		AffineTransform savedTransform = g2.getTransform();
		AffineTransform newTransform = (AffineTransform) g2.getTransform().clone();
		newTransform.rotate(angle,
				this.position.x + this.animation.getDimension().width / 2,
				this.position.y + this.animation.getDimension().height / 2);
		g2.setTransform(newTransform);
		
		g2.drawImage(this.animation.getImage(), (int) this.position.x,
				(int)this.position.y, new ImageObserver() {

					public boolean imageUpdate(Image img, int infoflags, int x,
							int y, int width, int height) {
						// TODO Auto-generated method stub
						return false;
					}
				});
		g2.setTransform(savedTransform);
	}
}
